package com.bugpool.leilema.freamwork.datascope;

import com.baomidou.mybatisplus.extension.api.R;
import com.bugpool.leilema.freamwork.datascope.impl.*;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wh
 * @Description
 * @Date 2023/6/14 11:16
 * @Version 1.0
 **/
@Component
public class DataScopeConfig {

    private final static Map<String, DataScopeStrategy> map = new ConcurrentHashMap<>();


    @PostConstruct
    public void init() {
        map.put(DataScopeConstants.DATA_SCOPE_ALL, new AllDataScope());
        map.put(DataScopeConstants.DATA_SCOPE_CUSTOM, new CustomDataScope());
        map.put(DataScopeConstants.DATA_SCOPE_DEPT, new DeptDataScope());
        map.put(DataScopeConstants.DATA_SCOPE_DEPT_AND_CHILD, new DeptAndChildDataScope());
        map.put(DataScopeConstants.DATA_SCOPE_SELF, new UserDataScope());
    }


    public final static DataScopeStrategy getDataScopeStrategy(String dataScope) {
        Optional.of(dataScope).orElseThrow(() -> new RuntimeException("未分配数据权限"));
        return Optional.of(map.get(dataScope))
                .orElseThrow(() -> new RuntimeException("未知的数据权限"));
    }


}
